//
//  ConversationCell.m
//  text888888
//
//  Created by lanou3g on 16/3/25.
//  Copyright © 2016年 田飞. All rights reserved.
//

#import "ConversationCell.h"
#import <AVIMConversation+Custom.h>
#import <CDChatManager.h>
#import <UIView+XHRemoteImage.h>
#import <CDMessageHelper.h>
#import <NSDate+DateTools.h>
@implementation ConversationCell

- (void)awakeFromNib {
    self.avatarImageView.clipsToBounds = YES;
    self.avatarImageView.image = [UIImage imageNamed:@"icon58"];
    _operationQueue = [[NSOperationQueue alloc] init];
   
//    _badgeView = [[JSBadgeView alloc]initWithParentView:self.timestampLabel alignment:JSBadgeViewAlignmentBottomCenter];
}

- (void)setConversation:(AVIMConversation *)conversation{

    if (conversation.type == CDConversationTypeSingle) {
        [self.operationQueue addOperationWithBlock:^{
            id <CDUserModelDelegate> user = [[CDChatManager manager].userDelegate getUserById:conversation.otherId];
            self.nameLabel.text = user.username;
                       [self.avatarImageView setImageWithURL:[NSURL URLWithString:user.avatarUrl]];
        }];
        
    } else {
        [self.avatarImageView setImage:conversation.icon];
        self.nameLabel.text = conversation.displayName;
       
    }
    if (conversation.lastMessage) {
        self.messageTextLabel.attributedText = [[CDMessageHelper helper] attributedStringWithMessage:conversation.lastMessage conversation:conversation];
            self.timestampLabel.text = [[NSDate dateWithTimeIntervalSince1970:conversation.lastMessage.sendTimestamp / 1000] timeAgoSinceNow];
        } if (conversation.unreadCount > 0) {
            if (conversation.muted) {
               
            } else {
//                self.badgeView.badgeText = [NSString stringWithFormat:@"%ld", (long)conversation.unreadCount];
            }
        }


}
+ (instancetype)cellForRowWithTableView:(UITableView *)tableView{
    
    NSString * className = NSStringFromClass([self class]);
    [tableView registerNib:[UINib nibWithNibName:className bundle:nil] forCellReuseIdentifier:className];
    return [tableView dequeueReusableCellWithIdentifier:className];

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
